const loaderUtils = require('loader-utils');
const { LoaderContext } = require('webpack');

/**
 * @this {LoaderContext<{name:string}>}
 * @param {string} source
 */
module.exports = function (source, sourceMap) {
  // const query = this.query // or:
  // const query = loaderUtils.getOptions(this);
  // console.log(query);

  const lines = source.split('\n').map((str) => str.trim());
  const obj = {};
  for (const line of lines) {
    if (line.startsWith('#') || !line.trim()) {
      continue;
    }
    const kvs = line.split(':').map((str) => str.trim());
    obj[kvs[0]] = kvs[1];
  }

  // return 'export default ' + JSON.stringify(obj);  // This is too simple!

  let c = this.async();
  console.log(c === this.callback); // true

  setTimeout(() => {
    this.callback(null, 'export default ' + JSON.stringify(obj), sourceMap);
  }, 2000);

  console.log(this.mode, this.target, this.sourceMap, this.rootContext, this.resourcePath, this.resourceQuery);
};
